﻿using System;
using System.Reflection;
using System.Xml;
using Syfuhs.APIWrappers.LiveMeeting;
using System.Collections;
using System.Collections.Generic;

namespace APITest
{
    class Program
    {
        static void Main(string[] args)
        {
            testc(null);

            Console.WriteLine();
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Fun with Live Meeting");
            Console.ResetColor();

            Console.WriteLine("=====================");

            LiveMeetingApiHandler handler = null;

            while (true)
            {
                bool tryAgain = true;

                Console.WriteLine();

                Console.Write("Conference Center: ");
                string confCenter = Console.ReadLine();

                Console.Write("API User: ");
                string user = Console.ReadLine();

                Console.Write("API Password: ");
                string pass = Console.ReadLine();

                try
                {
                    handler = new LiveMeetingApiHandler(confCenter, user, pass);
                    showLinks(handler.RetrieveCustomizedLinks());
                }
                catch { }

                do
                {
                    Console.WriteLine();

                    handler = new LiveMeetingApiHandler(confCenter, user, pass);

                    handleSelection(showMenu(), handler);
                }
                while (tryAgain);
            }
        }

        private static void testc(LiveMeetingApiHandler handler)
        {
            XmlDocument doc = new XmlDocument();
            //doc.LoadXml("<PlaceWareConfCenter><ListUsersReply><U>alice</U><U>stephen</U><U>phil</U><U>jack</U><U>terri</U><U>dave</U><U>joan</U><U>patty</U><U>kriste</U><U>mary</U><U>jean</U><U>lori</U><U>cathy</U><U>gloria</U><U>francis</U></ListUsersReply></PlaceWareConfCenter>");
            //doc.Load("xmlfile1.xml");
            //writeUserToConsole(LiveMeetingApiHandler.test(doc));
            //handler.test(doc);
        }

        private static void handleSelection(int selection, LiveMeetingApiHandler handler)
        {
            switch (selection)
            {
                case 0:
                    exit();
                    break;

                case 1:
                    viewMeeting(handler);
                    break;

                case 2:
                    createMeeting(handler);
                    break;

                case 3:
                    viewUser(handler);
                    break;
                case 4:
                    createUser(handler);
                    break;

                case 5:
                    viewAllUsers(handler);
                    break;

                case 6:
                    viewAllGroups(handler);
                    break;

                case 7:
                    viewConferenceCenterDetails(handler);
                    break;

                case 8:
                    searchMeetings(handler);
                    break;

                case 9:
                    createGroup(handler);
                    break;

                default:
                    break;
            }
        }

        private static void createGroup(LiveMeetingApiHandler handler)
        {
            Console.Write("Group Name: ");
            string name = Console.ReadLine();
            List<string> users = new List<string>();

            Console.WriteLine();
            Console.WriteLine("Enter a blank line when finished.");
            Console.WriteLine();

            while (true)
            {
                Console.Write("User: ");
                string user = Console.ReadLine();
                if (string.IsNullOrEmpty(user))
                    break;

                users.Add(user);
            }

            handler.CreateGroup(name, users.ToArray());
        }

        private static void searchMeetings(LiveMeetingApiHandler handler)
        {
            Console.Write("Search From: ");
            DateTime start = DateTime.Parse(Console.ReadLine());

            Console.Write("Search To: ");
            DateTime end = DateTime.Parse(Console.ReadLine());

            Console.Write("Include deleted Meetings (Y/N): ");
            bool included = Console.ReadLine().ToUpperInvariant().Equals("Y");

            foreach (var m in handler.GetMeetings(start, end, included))
                writePropertiesToConsole(m);
        }

        private static void viewConferenceCenterDetails(LiveMeetingApiHandler handler)
        {
            writePropertiesToConsole(handler.GetConferenceCenterData());
        }

        private static void viewAllGroups(LiveMeetingApiHandler handler)
        {
            var groups = handler.GetGroups();

            foreach (var group in groups)
                writePropertiesToConsole(group);
        }

        private static void viewAllUsers(LiveMeetingApiHandler handler)
        {
            var users = handler.GetUsers();

            foreach (var user in users)
                writeUserToConsole(user);
        }

        private static void viewUser(LiveMeetingApiHandler handler)
        {
            Console.Write("UserName: ");
            writeUserToConsole(handler.GetUser(Console.ReadLine()));
        }

        private static void writeUserToConsole(LiveMeetingUser user)
        {
            Console.WriteLine();
            writePropertiesToConsole(user);
            writePropertiesToConsole(user.Settings);
            Console.WriteLine();
        }

        private static void exit()
        {
            Environment.Exit(0);
        }

        private static void createUser(LiveMeetingApiHandler handler)
        {
            Console.Write("User Name: ");
            string user = Console.ReadLine();

            Console.Write("Email: ");
            string email = Console.ReadLine();

            Console.Write("Password: ");
            string password = Console.ReadLine();

            Console.Write("First Name: ");
            string firstName = Console.ReadLine();

            Console.Write("Last Name: ");
            string lastName = Console.ReadLine();

            Console.Write("Send Welcome Message (Y/N): ");
            bool sendMessage = Console.ReadLine().ToUpperInvariant().Equals("Y");

            writeUserToConsole(handler.CreateNewUser(user, password, firstName, lastName, email, sendMessage));
        }

        private static void createMeeting(LiveMeetingApiHandler handler)
        {
            Console.Write("Meeting Name: ");
            string name = Console.ReadLine();

            Console.Write("Title: ");
            string title = Console.ReadLine();

            Console.Write("Start: ");
            string start = Console.ReadLine();

            Console.Write("End: ");
            string end = Console.ReadLine();

            Console.Write("Max Users: ");
            string max = Console.ReadLine();

            writePropertiesToConsole(handler.CreateNewMeeting(name, title, DateTime.Parse(start), DateTime.Parse(end), int.Parse(max)));
        }

        private static void viewMeeting(LiveMeetingApiHandler handler)
        {
            Console.Write("Meeting Name: ");

            Meeting m = handler.GetMeeting(Console.ReadLine(), false);

            writePropertiesToConsole(m);
        }

        private static PropertyInfo[] getProperties(object o)
        {
            return o.GetType().GetProperties();
        }

        private static void writePropertiesToConsole(object o, string indent)
        {
            writePropertiesToConsole(o.GetType().GetProperties(), o, indent);
        }

        private static void writePropertiesToConsole(object o)
        {
            writePropertiesToConsole(o.GetType().GetProperties(), o, "");
        }

        private static void writePropertiesToConsole(PropertyInfo[] props, object o, string indent)
        {
            foreach (var p in props)
            {
                var val = p.GetValue(o, null);

                if (val == null)
                    continue;

                if (!(val is IEnumerable))
                {
                    Console.WriteLine(indent + p.Name + " = " + val);
                    continue;
                }

                if (val is string)
                    continue;

                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine(indent + p.Name + " = " + val);
                Console.ResetColor();

                foreach (var c in (IEnumerable)val)
                {
                    Console.WriteLine(indent + "      - " + c);

                    if (!IsValBclType(c))
                    {
                        writePropertiesToConsole(c, "     ");
                    }
                }
            }
        }

        private static bool IsValBclType(object val)
        {
            return val.GetType().Namespace.Contains("System");
        }

        private static int showMenu()
        {
            while (true)
            {
                Console.WriteLine("0. Exit");
                Console.WriteLine("1. View Meeting Details");
                Console.WriteLine("2. Create New Meeting");
                Console.WriteLine("3. View User details");
                Console.WriteLine("4. Create New User");
                Console.WriteLine("5. List All Users");
                Console.WriteLine("6. List All Groups");
                Console.WriteLine("7. View Conference Center Details");
                Console.WriteLine("8. Search Meetings");
                Console.WriteLine("9. Create Group");

                Console.WriteLine();
                Console.Write("Choice: ");

                int val = -1;

                if (int.TryParse(Console.ReadLine(), out val) && val <= 9)
                    return val;
            }
        }

        private static void showLinks(CustomizedLinks customizedLinks)
        {
            Console.WriteLine();
            Console.WriteLine("Check System: " + customizedLinks.CheckYourSystemLink);
            Console.WriteLine("Download: " + customizedLinks.DownloadLink);
            Console.WriteLine("Easy Assist: " + customizedLinks.EasyAssistJoinLink);
            Console.WriteLine("Help: " + customizedLinks.HelpLink);
            Console.WriteLine("Log On: " + customizedLinks.LoginLink);
            Console.WriteLine("Support: " + customizedLinks.SupportLink);
            Console.WriteLine("Terms of Service: " + customizedLinks.TermOfServiceLink);
            Console.WriteLine("----------------------------" + Environment.NewLine);
        }
    }
}
